#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "textwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    dialogUI(new DialogUI),
    timer(new QTimer(this)),
    sPieChart(new SPieChart),
    bitmapInode(new BitmapInode),
    bitmapBlock(new BitmapBlock)
{
    ui->setupUi(this);
    // Force fixed size window
    QWidget::setFixedSize(800, 600);

     QObject::connect(timer, SIGNAL(timeout()), this, SLOT(update()));
     timer->start(500);

    QObject::connect(ui->actionFormater, SIGNAL(activated()), dialogUI, SLOT(format()));

    QObject::connect(ui->actionNaviguer_versDossier, SIGNAL(activated()), dialogUI, SLOT(cdDirectory()));
    QObject::connect(ui->actionNouveauDossier, SIGNAL(activated()), dialogUI, SLOT(addDirectory()));
    QObject::connect(ui->actionSupprimerDossier, SIGNAL(activated()), dialogUI, SLOT(delDirectory()));

    QObject::connect(ui->actionNouveauFichier, SIGNAL(activated()), dialogUI, SLOT(addFile()));
    QObject::connect(ui->actionSupprimerFichier, SIGNAL(activated()), dialogUI, SLOT(delFile()));
    QObject::connect(ui->actionOuvrirFichier, SIGNAL(activated()), dialogUI, SLOT(openFile()));
    QObject::connect(ui->actionCopierFichier, SIGNAL(activated()), dialogUI, SLOT(cpFile()));

    QObject::connect(ui->actionA_propos, SIGNAL(activated()), dialogUI, SLOT(aboutDialog()));
    QObject::connect(ui->actionA_propos_de_Qt, SIGNAL(activated()), dialogUI, SLOT(aboutQtDialog()));

    QObject::connect(ui->inodeScrollBar, SIGNAL(valueChanged(int)), bitmapInode, SLOT(setScrollBarValue(int)));
    QObject::connect(ui->blockScrollBar, SIGNAL(valueChanged(int)), bitmapBlock, SLOT(setScrollBarValue(int)));

    sPieChart->setParent(ui->sPieChartBox);
    bitmapInode->setParent(ui->inodeBox);
    bitmapBlock->setParent(ui->blockBox);
    setAutoFillBackground(true);



    setUp();
}

void MainWindow::setUp()
{
    if (load_disk_hard_drive() != RET_SUCCESS) {
        mini_format();
        save_disk_hard_drive();
    }
}

void MainWindow::updateTree()
{
    FileList * L = NULL;
    FileList * P = NULL;

    Inode I;
    int ret;

    directory_get_list_from_file(get_current_dir(), &L);

    P = L;

    if(P == NULL ){}
    else
    {
        ui->textEdit->setText("");
        while(P != NULL)
        {
            ret = inode_get_from_disk(&I, P->address);
            if (ret < 0) {}
            else
            {
                QDateTime date;
                date.setTime_t(I.lastAccess);

                if(I.type == 1)//fichier
                { ui->textEdit->setText(ui->textEdit->toPlainText()+(P->name)+"   Fichier   "+ QString::number(I.size)+"    "+ (date.toString("dd/MM/yyyy hh:mm:ss")) +"\n");}
                else if(I.type == 2)
                { ui->textEdit->setText(ui->textEdit->toPlainText()+(P->name)+"   Dossier    "+ QString::number(I.size)+"    "+ (date.toString("dd/MM/yyyy hh:mm:ss")) +"\n");}
                else
                { ui->textEdit->setText(ui->textEdit->toPlainText()+(P->name)+ " Impossible de lire"+"\n");}
            }

            P = P->next;
        }
     }

    directory_clear_list(L);

}

void MainWindow::update()
{
    updateTree();
}

void MainWindow::keyPressEvent(QKeyEvent *event)
{
    if (event->key() == Qt::Key_Escape)
        close();
}

void MainWindow::closeEvent(QCloseEvent *event)
{
    // On sauvegarde le disque.
    if (save_disk_hard_drive() != RET_SUCCESS)
        QMessageBox::warning(NULL, "Clever File System", "Erreur lors de la sauvegarde du RAM-disk.");

    event->accept();
}

MainWindow::~MainWindow()
{
    delete ui;
}
